package one.bugu.android.demon.common;


import android.content.Intent;

import com.juefeng.android.framework.http.responce.HttpAsycResponceHandler;
import one.bugu.android.demon.MyApplication;
import one.bugu.android.demon.ui.activity.MainActivity;
import one.bugu.android.demon.ui.dialog.DialogHint;
import one.bugu.android.demon.util.PreferencesUtil;

/**
 * Created with IntelliJ IDEA.
 * User: LangK
 * Date: 2017/12/13
 * Time: 10:57
 * Description:网络请求返回时，可以拦截做基础的事情
 */
public class BaseHttpAsycResponceHandler<T> extends HttpAsycResponceHandler<T> {


    private boolean isError = false;

    public BaseHttpAsycResponceHandler(boolean isShowProgress) {
        super(isShowProgress);
    }

    public BaseHttpAsycResponceHandler() {
        super(false);
    }

    @Override
    public void onFailed(int code, String message) {
        super.onFailed(code, message);
        if (code==-2){
            DialogHint dialogHint = new DialogHint(MyApplication.getInstance().getCurrentActivity(),"登陆失效，请重新登陆",false);
            dialogHint.setOnDialogVersionListener(new DialogHint.OnDialogVersionListener() {
                @Override
                public void onSure() {
                    PreferencesUtil.getInstance().putBoolean(MyApplication.getInstance().getCurrentActivity(), "isFirst", false);
                    Intent intent = new Intent(MyApplication.getInstance().getCurrentActivity(),MainActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK|Intent.FLAG_ACTIVITY_NEW_TASK);
                    MyApplication.getInstance().getCurrentActivity().startActivity(intent);
                }
                @Override
                public void onCancel() {
                }
            });
            dialogHint.show();

        }
    }

    @Override
    public void onSuccess(T t) {
        super.onSuccess(t);
        isError = false;
    }

    @Override
    public void onError() {
        super.onError();
        isError = true;
    }


    public boolean isError() {
        return isError;
    }

    public void setError(boolean error) {
        isError = error;
    }
}
